package cn.tedu.sp02.controller;

import cn.tedu.sp01.entity.Item;
import cn.tedu.sp01.service.ItemService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * @Author 作者：小龙猿
 * @Project 项目：springcloud1
 * @Time 时间：2021/9/9 11:52
 */
@RestController
@Slf4j
public class ItemController {
    @Resource
    private ItemService itemService;
    @GetMapping("/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId) throws InterruptedException {
        List<Item> items = itemService.getItems(orderId);
        //随机阻塞,90%概率执行阻塞代码
        if (Math.random() < 0.9){
            //随机延迟时长,0到5秒
            int t = new Random().nextInt(5000);
            log.info("延迟: "+t);
            Thread.sleep(t);
        }
        return JsonResult.ok().data(items);
    }
    @PostMapping("/decreaseNumber")
    public JsonResult decreaseNumber(@RequestBody List<Item> list){
        itemService.decreaseNumbers(list);
        return JsonResult.ok().msg("减少库存成功");
    }
    @GetMapping("/favicon.ico")
    public void ico(){

    }
}
